<?php
$extra = '';
if (isset($_POST['chat_box']) && !empty($_POST['chat_box']) && $_POST['chat_box'] == 'Siųsti' && !empty($_POST['msg']) && isset($_SESSION['username']) && !empty($_SESSION['username'])) {
	$msg = htmlspecialchars($_POST['msg']); $nick = $_SESSION['username']; $nick_id = $_SESSION['id'];
	mysql_query1("INSERT INTO `chat_box` (`nikas`, `msg`, `time`, `niko_id`) VALUES (".escape($nick).", ".escape($msg).", NOW(), ".escape($nick_id).");");
}
function chatbox() {
	global $conf; $extra = '';
	if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
		$chat_box = '<form name="chat_box" action="" method="post">
		<textarea name="msg" rows="3" cols="10" style="width:95%"></textarea>
		<br />
		<input type="submit" name="chat_box" value="Siųsti" />
		</form>
		';
	}
	else {
		$chat_box = '<textarea name="chat" rows="3" cols="10" style="width:95%" disabled="disabled">Turite prisijungti.</textarea>
		<br />
		<input type="submit" name="chat_box" value="Siųsti" disabled="disabled" />
	';
	}
	$chat_box .= "<hr />"; $chat = mysql_query1("SELECT * FROM `chat_box` ORDER BY `time` DESC LIMIT ".$conf['Chat_limit']."");
	while($row = mysql_fetch_assoc($chat)) {
		if (isset($_SESSION['username']) && $row['nikas'] != $_SESSION['username']) { $extra = "<a href=\"?id,45;n,1;u,".$row['nikas']."\"><img src=\"images/pm/mail.png\" alt=\"pm\" border=\"0\" /></a> "; }
		if (defined("LEVEL") && LEVEL == 30) { $extra .= "<a href='?id,49;d,".$row['id']."'>[d]</a> <a href='?id,49;r,".$row['id']."'>[r]</a> <a href='?id,49;b,".$row['niko_id']."'>[b]</a>"; }
		$chat_box .= "<div class=\"sarasas\"><a href=\"?id,47;m,".$row['niko_id']."\" title=\"header=[".$row['nikas']."] body=[<br/>Data: <b>".$row['time']."</b><br/>Kada: <b>".kada($row['time'])."</b><br/><br/>] cssbody=[sarasas] cssheader=[title] fade=[on]\">".$row['nikas']."</a> $extra<br />".smile(bbchat(wrap($row['msg'],25)))."</div>";
		$extra = '';
	}
	if (isset($_SESSION['username'])) { $chat_box .= "<a href=\"?id,49\" title=\"Archyvas\">Archyvas</a>"; }
	return $chat_box;	
}
//CHAT_BOX
lentele("Šaukykla",chatbox());
?>
